package gw.com.android.ui.me_concern;

import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.gwtsz.gts2.hx.R;

import www.com.library.view.XRecyclerView;

public class MyConcernRecyclerView extends XRecyclerView {

    public MyConcernRecyclerView(Context context) {
        this(context, null);
    }

    public MyConcernRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyConcernRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            MyConcernHorizontalSlideLayout openedChild = null;
            for (int i = 0; i < getChildCount(); i++) {
                MyConcernHorizontalSlideLayout layout = (MyConcernHorizontalSlideLayout) getChildAt(i).findViewById(R.id.layout_slide);
                if (layout == null) continue;
                if (layout.mStatus == MyConcernHorizontalSlideLayout.STATUS_EXPAND) {
                    openedChild = layout;
                }

                //下面的代码，保证每一次最多打开一个menu
                //找到centerView的点击范围，而是要找Menu的点击范围
                if (null != openedChild) {
                    boolean ifClickMenu = false;
                    for (int j = 0; j < openedChild.getMenuViewItemList().size(); j++) {
                        View v = openedChild.getMenuViewItemList().get(j);//因为menu可能有多个
                        if (null != v) {
                            if (ifClickInsideView(v, e)) {
                                ifClickMenu = true;
                                break;
                            } else {
                                ifClickMenu = false;
                            }
                        }
                    }
                    if (!ifClickMenu)
                        layout.hideMenu();
                }

            }
        }
        return super.onInterceptTouchEvent(e);
    }

    private boolean ifClickInsideView(View view, MotionEvent event) {//获取view的矩形范围
        Rect temp = new Rect();// 构造一个矩形
        view.getGlobalVisibleRect(temp);// 调用view的api，用view的实际可见绘制矩形区域 来重新定义这个矩形(注意，这里是相对于整个绘制坐标轴的矩形，而不是相对于父组件)
        return temp.contains((int) event.getRawX(), (int) event.getRawY());//由于上面用的是相对于整个坐标轴的矩形区域，
        // 这里要使用全局X,Y坐标，(getRawX和getRawY是全局的，而getX和getY是获取相对于父组件的坐标值)
    }
}
